package nix.upload.servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import sun.misc.BASE64Encoder;

public class DownUtils {

	/**
	 * 文件下载文件名编码
	 */
	public static String fileNameEncoding(String fileName,
			HttpServletRequest request) throws IOException {
		String agent = request.getHeader("User-Agent");
		if (agent.toLowerCase().contains("firefox")) {
			BASE64Encoder base64Encoder = new BASE64Encoder();
			fileName = "=utf-8?B?"
					+ base64Encoder.encode(fileName.getBytes("utf-8")) + "?=";
		} else if (agent.toLowerCase().contains("msie")) {
			fileName = URLEncoder.encode(fileName, "utf-8");
		} else {
			fileName = URLEncoder.encode(fileName, "utf-8");
		}
		return fileName;
	}
}
